package cn.kgc.itrip.util;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.Set;
import java.util.concurrent.TimeUnit;

public class RedisUtil {

    private StringRedisTemplate stringRedisTemplate;
    private RedisTemplate redisTemplate;

    public void setStringRedisTemplate(StringRedisTemplate stringRedisTemplate){
        this.stringRedisTemplate=stringRedisTemplate;
    }

    public void setRedisTemplate(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }


    /**
     * 添加 and 修改
     */
    public boolean setValue(String key,String value){
        try {
            stringRedisTemplate.opsForValue().set(key,value);
            return true;
        }catch (Exception e){
            return false;
        }
    }


    /**
     * 添加设置时间
     */
    public boolean setValueExpire(String key,String value,long expire){
        try {
            stringRedisTemplate.opsForValue().set(key,value,expire,TimeUnit.SECONDS);
            return true;
        }catch (Exception e){
            return false;
        }
    }


    /**
     * 删除
     */
    public boolean del(String key){
        try {
            stringRedisTemplate.delete(key);
            return true;
        }catch (Exception e){
            return false;
        }
    }


    /**
     * 删除key集合
     */
    public boolean delList(String... key){
        try {
            for (String k:key) {
                stringRedisTemplate.delete(k);
            }
            return true;
        }catch (Exception e){
            return false;
        }
    }


    /**
     * 查询一个
     */
    public String getValue(String key){
          return  stringRedisTemplate.opsForValue().get(key);
    }


    /**
     * 查询全部
     */
    public Set<String> getValueList(String pattern){
           return stringRedisTemplate.keys(pattern);
    }





}
